home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / PowerPlant / AGA Classes 1.2 / Text / LAGAEditField.cp < prev    next >
Encoding:
Text File  |  1996-06-30  |  10.2 KB  |  314 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    LAGAEditField.cp
  3. // ===========================================================================
  4. //    “Apple Grayscale Appearance” compliant Text Entry Field
  5. //    Copyright © 1996 Chrisoft (Christophe ANDRES)  All rights reserved.
  6. //
  7. //    You may use this source code in any application (commercial, shareware, freeware,
  8. //    postcardware, etc), but not remove this notice (no need to acknowledge the use of
  9. //    this class in the about box)
  10. //    You may not sell this source code in any form. This source code may be placed on 
  11. //    publicly accessable archive sites and source code disks. It may not be placed on 
  12. //    profit archive sites and source code disks without the permission of the author, 
  13. //    Christophe ANDRES.
  14. //    
  15. //        This source code is distributed in the hope that it will be useful,
  16. //        but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. //        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. //
  19. //    If you make any change or improvement on this class, please send the improved/changed
  20. //    version to : chrisoft@calva.net or Christophe ANDRES
  21. //                                     20, rue Prosper Mérimée
  22. //                                     67100 STRASBOURG
  23. //                                     FRANCE
  24. //
  25. // ===========================================================================
  26. //    LAGAEditField.h            <- double-click + Command-D to see class declaration
  27. //
  28. //    LAGAEditField is my implementation of the “Apple Grayscale Appearance for System 7.5”
  29. //        Text Entry field. It handles the 3D effect, but is also designed to work effectively
  30. //        with a class like LAGAIndexTab. In an index tab, a Text Entry Field can be present but
  31. //        in another Index Tab, and should therefore not be selected when the user presses the
  32. //        “Tab” key. Also if the Index Tab is changed and a Text Entry Field is revealed it should
  33. //        be selected. LAGAEditField handles all these cases.
  34. //        LAGAEditField keeps its own background in white, whatever the window background may be.
  35. //
  36. //        This class requires AGAColors.cp to be present in your project
  37. //
  38. //        Version : 1.2
  39. //
  40. //        Change History (most recent first, date in US form : mm/dd/yy):
  41. //
  42. //                        06/30/96    ca        Public release of version 1.2
  43. //                        06/26/96    ca        Changed the way the disabled state is drawn (border in real gray and text also)
  44. //                        06/24/96    ca        Added call to StColorPenState::Normalize to avoid influence on our colors
  45. //                        06/20/96    vp        Vince Parsons <vparsons@ddg.com>
  46. //                                                        Changed the rect erase to white to avoid an unwanted gray border
  47. //                        06/04/96    ca        Added RegisterClass method to ease registry
  48. //                                                        Increased version to 1.2
  49. //                        05/20/96    ca        Increased version to 1.1
  50. //                                                        Added copy constructor
  51. //                                                        Added "on the fly" constructor
  52. //                                                        Replaced UEnvironment::HasFeature(env_SupportsColor) with PaneInColor
  53. //                                                        Added change history
  54. //                        05/10/96    M™H        Changes by Michael(tm) Hamel <mhamel@adi.co.nz>
  55. //                                                        Corrected the fact that LAGAEditField used to draw outside of its frame, which is a problem
  56. //                                                            if you Hide() it.
  57. //                                                        Note : You might need to increase slightly (2 pixels) the height of objects created with
  58. //                                                                        version 1.0
  59. //                        04/22/96    ca        class made available by Christophe ANDRES <chrisoft@calva.net>
  60. //                                                        (version 1.0)
  61. //
  62. //        To Do:
  63. //
  64.  
  65. #include "LAGAEditField.h"
  66. #include "AGAColors.h"
  67. #include <UDrawingState.h>
  68. #include <UEnvironment.h>
  69. #include <PP_Messages.h>
  70. #include <LView.h>
  71.  
  72. //    begin    <06/04/96    ca>
  73. void LAGAEditField::RegisterClass ()
  74.  
  75. {
  76.     URegistrar::RegisterClass(LAGAEditField::class_ID, (ClassCreatorFunc)LAGAEditField::CreateAGAEditFieldStream);
  77. }
  78. //    end    <06/04/96    ca>
  79.  
  80. LAGAEditField* LAGAEditField::CreateAGAEditFieldStream (LStream *inStream)
  81.  
  82. {
  83.     return(new LAGAEditField(inStream));
  84. }
  85.  
  86. //-------Constructors-------------------------------------------------------------------------------------------------
  87.  
  88. LAGAEditField::LAGAEditField (LStream *inStream) : LEditField (inStream)
  89.  
  90. {
  91.     AlignTextEditRects();    // Our version...                                                                                            <05/10/96    M™H>
  92. }
  93.  
  94. LAGAEditField::LAGAEditField (const LAGAEditField &inOriginal) : LEditField(inOriginal)
  95.  
  96. {
  97.     AlignTextEditRects();    // Our version...                                                                                            <05/10/96    M™H>
  98. }
  99.  
  100. LAGAEditField::LAGAEditField (const SPaneInfo &inPaneInfo, Str255 inString, ResIDT inTextTraitsID,
  101.                                                             Int16 inMaxChars, Boolean inHasBox, Boolean inHasWordWrap,
  102.                                                             KeyFilterFunc    inKeyFilter, LCommander *inSuper)
  103.                                                         :LEditField(inPaneInfo, inString, inTextTraitsID, inMaxChars, inHasBox,
  104.                                                                                 inHasWordWrap, inKeyFilter, inSuper)
  105. {
  106.     AlignTextEditRects();    // Our version...                                                                                            <05/10/96    M™H>
  107. }
  108.  
  109. void LAGAEditField::DrawBox ()
  110.  
  111. {
  112.     Rect frame;
  113.     Boolean hasColor = ::PaneInColor(this);
  114.     StColorPenState saveColors;
  115.  
  116.     saveColors.Normalize();                                                                                                                        //    <06/24/96    ca>
  117.     CalcLocalFrameRect(frame);
  118.     if (hasColor)
  119.         {
  120.             ::RGBForeColor(&gAGAColorArray[5]);
  121.             ::MoveTo(frame.left, frame.bottom - 2);
  122.             ::LineTo(frame.left, frame.top);
  123.             ::LineTo(frame.right - 2, frame.top);
  124.             ::ForeColor(whiteColor);
  125.             ::MoveTo(frame.left + 1, frame.bottom - 1);
  126.             ::LineTo(frame.right - 1, frame.bottom - 1);
  127.             ::LineTo(frame.right - 1, frame.top + 1);
  128.         }
  129.     
  130.     // Inner black or gray border
  131.     ::InsetRect(&frame, 1, 1);
  132.     ApplyForeAndBackColors();
  133.     if (mEnabled != triState_On)
  134.         if (hasColor)
  135.             ::RGBForeColor(&gAGAColorArray[8]);                                                                                        //    <06/26/96    ca>
  136.         else
  137.             ::PenPat(&UQDGlobals::GetQDGlobals()->gray);
  138.     ::FrameRect(&frame);
  139.  
  140.     // TextEdit area and inner border, white
  141.     ::InsetRect(&frame, 1, 1);                                                                                                                //    <06/20/96    vp>
  142.     ::BackColor(whiteColor);
  143.     ::EraseRect(&frame);
  144. }
  145.  
  146. //-------------- From LEditField, patched for 3-deep indent -------------------
  147.  
  148. void LAGAEditField::DrawSelf ()
  149.  
  150. {
  151.     Rect    frame;
  152.     StColorPenState saveColors;
  153.     
  154.     saveColors.Normalize();                                                                                                                        //    <06/26/96    ca>
  155.     CalcLocalFrameRect(frame);
  156.     
  157.     if (mHasBox)                // Draw Border if necessary
  158.         {
  159.             DrawBox();
  160.             ::InsetRect(&frame, 3, 3);
  161.         }
  162.  
  163.     if ((mEnabled != triState_On) && ::PaneInColor(this))                                                            //    <06/26/96    ca>
  164.         ::RGBForeColor(&gAGAColorArray[8]);                                                                                            //    <06/26/96    ca>
  165.  
  166.         // A Mac TERec stores a pointer to its owner port  We have to
  167.         // change it to the current port in case we are drawing into
  168.         // a port that is not the owner port. This happens when we are
  169.         // printing or drawing into an offscreen port.
  170.         
  171.     GrafPtr    savePort = (**mTextEditH).inPort;
  172.     (**mTextEditH).inPort = UQDGlobals::GetCurrentPort();
  173.  
  174.     ::TEUpdate(&frame, mTextEditH);
  175.     
  176.     (**mTextEditH).inPort = savePort;
  177. }
  178.  
  179. void LAGAEditField::AlignTextEditRects ()
  180.  
  181. {
  182.     Rect    textFrame;
  183.     if (!CalcLocalFrameRect(textFrame))
  184.         {
  185.         
  186.                 // Frame is outside QD Space. Put textFrame  at the
  187.                 // upper left limit of QD Space (extreme negative coords).
  188.                 // That location is guaranteed to be offscreen (unless
  189.                 // you have a control longer than 32K pixels) since PP
  190.                 // Image coordinates start at (0,0) and are never negative.
  191.  
  192.             textFrame.left = min_Int16;
  193.             textFrame.right = textFrame.left + mFrameSize.width;
  194.             textFrame.top = min_Int16;
  195.             textFrame.bottom = textFrame.top + mFrameSize.height;
  196.         }
  197.     
  198.     if (mHasBox)
  199.         {
  200.             ::InsetRect(&textFrame, 3, 3);        // M™H for LAGA version
  201.         }
  202.                                     // Set TextEdit view and dest rectangles
  203.                                     //   to be the same as the Frame
  204.     (**mTextEditH).viewRect = textFrame;
  205.     (**mTextEditH).destRect = textFrame;
  206.     
  207.     AdjustTextWidth(false);
  208.  
  209.     ::TECalText(mTextEditH);        // Let TextEdit adjust line breaks
  210. }
  211.  
  212. void LAGAEditField::AdjustTextWidth (Boolean inShrinkToText)
  213. {
  214.     if (!mHasWordWrap)            // Adjust only if WordWrap is OFF
  215.         {
  216.         
  217.             Rect    textFrame;            // Get size of editable text area
  218.             CalcLocalFrameRect(textFrame);
  219.             if (mHasBox)
  220.                 {
  221.                     ::InsetRect(&textFrame, 3, 3);
  222.                 }
  223.         
  224.             Int16    destWidth = 4000;    // Very Wide
  225.         
  226.             if (inShrinkToText)        // Width of the Text in the EditField
  227.                 {
  228.  
  229.                     Point    startPoint = ::TEGetPoint(0, mTextEditH);
  230.                     Point    endPoint = ::TEGetPoint((**mTextEditH).teLength, mTextEditH);
  231.                     destWidth = endPoint.h - startPoint.h;
  232.             
  233.                     if (destWidth < textFrame.right - textFrame.left)
  234.                         {
  235.                             destWidth = textFrame.right - textFrame.left;
  236.                         }
  237.                 }
  238.         
  239.                                     // Direction to extend dest rect depends
  240.                                     //   on the text justification
  241.             Int16    just = (**mTextEditH).just;
  242.             if (just == teFlushDefault)
  243.                 {
  244.                                             // For left justificaton, GetSysDirection
  245.                                             //    returns teFlushDefault
  246.                                             //    For right, teFlushRight
  247.                     just = ::GetSysDirection();
  248.                 }
  249.                 
  250.             switch (just)
  251.                 {
  252.                 
  253.                     case teFlushLeft:        // Text fixed on left and grows right
  254.                     case teFlushDefault:
  255.                         (**mTextEditH).destRect.right = (**mTextEditH).destRect.left + destWidth;
  256.                         break;
  257.                         
  258.                     case teFlushRight:        // Text grows to the left
  259.                         (**mTextEditH).destRect.left = (**mTextEditH).destRect.right - destWidth;
  260.                         break;
  261.                         
  262.                     case teCenter:
  263.                         {        // Text grows left and right
  264.                             Int16    center = (textFrame.left + textFrame.right) / 2;
  265.                             (**mTextEditH).destRect.left = center - 2000;
  266.                             (**mTextEditH).destRect.right = center + 2000;
  267.                             break;
  268.                         }
  269.                 }
  270.         }
  271. }
  272.  
  273. //-------------------------------------------------------------------------
  274.  
  275. Boolean LAGAEditField::ObeyCommand (CommandT inCommand, void* ioParam)
  276.  
  277. {
  278.     Boolean cmdHandled = true;
  279.  
  280.     switch (inCommand)
  281.         {
  282.             case msg_TabSelect    :            //    Often LAGAEditField items can be in tab index views and we need to be seen to be selected
  283.                         if (!IsEnabled())
  284.                             cmdHandled = false;
  285.                         else
  286.                             if (mSuperView != nil)
  287.                                 {
  288.                                     Rect frame;
  289.                                     Rect revealedRect;
  290.                                 
  291.                                     CalcPortFrameRect(frame);
  292.                                     mSuperView->GetRevealedRect(revealedRect);
  293.                                     cmdHandled = ::SectRect(&revealedRect, &frame, &revealedRect);
  294.                                 }
  295.                         if (!cmdHandled)
  296.                             break;                    //    else fall thru to default case
  297.             default:
  298.                         cmdHandled = LEditField::ObeyCommand(inCommand, ioParam);
  299.                         break;
  300.         }
  301.     
  302.     return cmdHandled;
  303. }
  304.  
  305. void LAGAEditField::SpendTime (const EventRecord    &inMacEvent)
  306.  
  307. {
  308.     if (FocusExposed())
  309.         ::TEIdle(mTextEditH);
  310.     else
  311.         SwitchTarget(GetSuperCommander());
  312. }
  313.  
  314.